# -*- coding:utf-8 -*-
from STool.StockFileManager import StockFileManager
from STool.StockTool import StockTool
from STool.StockTT import *

class StockAccount:
    def __init__(self,sql):
        self.mysql = sql
        self.today_string= StockTool.getTodayString()
    
    
    def unifiedDivision(self,maxNum):
        
        for i in range(0,15):
            if maxNum % 10 != 0:
                maxNum = maxNum+1
            else:
                return maxNum / 5
        return maxNum

    def getAccountInfo(self):
        aa = self.mysql.searchAccount()
        aa = aa.sort_values(by=['trade_date'],ascending=True) 
        date = list(aa['trade_date'])
        zhishu = list(aa['shangzheng'])
        account = list(aa['account'])
        print(date)
        print(zhishu)
        print(account)
        
        datasets=[]
        dic={}
        dic["label"]="账户净值"
        dic["fill"]=False
        dic["data"]=account
        dic["borderColor"]= "#DC143C"
        dic["backgroundColor"]="#DC143C"
        dic["borderWidth"]=1.5
        dic["pointRadius"]=1.7
        dic["yAxisID"]="A"
        
        dic1={}
        dic1["label"]="上证指数"
        dic1["fill"]=False
        dic1["data"]=zhishu
        dic1["borderColor"]= "#000080"
        dic1["backgroundColor"]="#000080"
        dic1["borderWidth"]=1.5
        dic1["pointRadius"]=1.7
        dic1["yAxisID"]="B"
        
        
        datasets.append(dic)
        datasets.append(dic1)
        
        chartdata = {}
        chartdata['labels']=date
        chartdata['datasets']=datasets

        options = {
    "stacked": False,
    "title": {
      "text": "rps5",
      "display": True
    },
    "maintainAspectRatio": False,
    "animation": {
      "duration": 0
    },
    "responsive": True,
    "scales": {
      "xAxes": [
        {
          "offset":1,
          "gridLines": {
            "display": False
          }
        }
      ],
      "yAxes": [
        {
         
          "id": "A",
          "ticks": {
            "min": 4.6,
            "max": 9.0,
            "stepSize": 0.31
          },
          "splitNumber":0,

        },
        {
          "id": "B",
          "splitNumber":0,
          "position": "right",
          "gridLines": False,
          "ticks": {
            "min": 2500,
            "max": 4900,
            "stepSize": 150
         },
          "pad":{
            "left":10
          },
          "grid":{
          }

        }
      ]
    }
  }


        account_data={
            "chartdata":chartdata,
            "options":options
        }
        StockFileManager.rewriteDataToFile(account_data,"account")

        
        
        
    def rewriteToLocal(self,STOCK_RPS,fileName,code_names):
        chartdata={}
        chartdata["labels"]= (STOCK_RPS.columns).tolist()
        array=[]
        n = 0
        for th_code in STOCK_RPS.index:
            color=StockTool.getLocalColor(n)
            code_name = code_names[th_code]            
            dic={}
            dic["label"]=code_name
            dic["fill"]=False
            dic["data"]=STOCK_RPS.loc[th_code].tolist()
            dic["borderColor"]=color
            dic["backgroundColor"]=color
            dic["borderWidth"]=2.2
            array.append(dic)
            n+=1

        chartdata["datasets"]=array
        options={}
        
        options["maintainAspectRatio"]= False
        options["responsive"]= True
        options["hoverMode"]= "index"
        options["stacked"]= False
        options["title"]= {"display":True,"text":fileName}
        JSON_DATA={
            "chartdata":chartdata,
            "options":options
        }
        StockFileManager.rewriteDataToFile(JSON_DATA,fileName)

        